﻿using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;

namespace Mars.GpuInstance
{
    /// <summary>
    /// 已经合并到GPUAnimatorControllerJob，这个暂时没有使用
    /// </summary>
    public struct GPUBonesFixWeightsJob : IJobParallelFor
    {
        [NativeDisableContainerSafetyRestriction]
        public NativeArray<float4> gpuiAnimationData;            // index: 0 x -> frameNo1, y -> frameNo2, z -> frameNo3, w -> frameNo4
                                                                 // index: 1 x -> weight1, y -> weight2, z -> weight3, w -> weight4
        public void Execute(int index)
        {
            int weightIndex = index * 2 + 1;
    
            float4 weightData = gpuiAnimationData[weightIndex];
            float totalWeight = weightData.x;
            totalWeight += weightData.y;
            totalWeight += weightData.z;
            totalWeight += weightData.w;
    
            if (totalWeight <= 0)
            {
                weightData[0] = 1;
            }
            else
            {
                totalWeight = 1 / totalWeight;

                weightData.x *= totalWeight;
                weightData.y *= totalWeight;
                weightData.z *= totalWeight;
                weightData.w *= totalWeight;
            }

            gpuiAnimationData[weightIndex] = weightData;
        }
    }
}